package com.teampuzzle;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

public class Player {

	private int score;
	public int level;
	private int levelScore;
	private int nextLevelPoints;
	private int currentLevelPoints;
	private float time;
	public int multiplier;
	public int streak;
	private long lastTime;
	Paint scorePaint;
	Paint TimeRectangle;
	Paint levelBarPaint;
	Paint levelBackPaint;
	Paint levelTextPaint;
	Paint linePaint;
	Paint multiplierPaint;
	Paint endPaint;
	Paint levelPaint;
	Paint scoreTextPaint;
	Paint TutorialText;
	Bitmap[] timeTexture = new Bitmap[15];
	int displayWidth;
	int displayHeight;
	public int addedscore = 0;
	
	//Koordinater f�r top-bar
	int levelBarX;
	int levelBarY;
	int levelBarScoreX;
	int levelBarScoreY;
	int barCenterX;
	int levelBarLength;
	int levelBarHeight;
	int levelBarProgress;
	int levelTextSize;
	int scoreTextSize;
	
	int[] levelValues;
	//= {0, 200, 400, 800};
	
	Bitmap timeBM;
	Bitmap timeBM_red;
	Bitmap timeBM_blue;

	boolean restartButtonPressed;
	
	public Player(){
		score 	= 0;
		level 	= 1;
		time 	= (float) 15.0;
		multiplier = 1;
		streak = 0;
		levelScore = 0;
		nextLevelPoints = 100;
		currentLevelPoints = 0;
		
		levelBarProgress = 0;
		
		restartButtonPressed = false;
		
		//V�rden f�r levels
		//int[] levelValues = {300, 800, 2000};
		
		displayWidth = TeampuzzleActivity.displayWidth;
		displayHeight = TeampuzzleActivity.displayHeight;
		
		levelTextSize = (int) ((double) displayWidth*0.04);
		scoreTextSize = (int) ((double) displayWidth*0.05);
		
		scorePaint = new Paint();
		scorePaint.setARGB(255, 191, 198, 221);
		scorePaint.setAntiAlias(true);
		scorePaint.setTextSize(scoreTextSize);//20
		scorePaint.setTextAlign(Paint.Align.RIGHT);
		scorePaint.setFakeBoldText(true);
		
		scoreTextPaint = new Paint();
		scoreTextPaint.setARGB(255, 109, 204, 122);
		scoreTextPaint.setAntiAlias(true);
		scoreTextPaint.setTextSize(scoreTextSize);//20
		scoreTextPaint.setTextAlign(Paint.Align.RIGHT);
		scoreTextPaint.setFakeBoldText(true);
		
		levelPaint = new Paint();
		levelPaint.setARGB(255, 191, 198, 221);
		levelPaint.setAntiAlias(true);
		levelPaint.setTextSize(scoreTextSize+5);//20
		levelPaint.setTextAlign(Paint.Align.RIGHT);
		levelPaint.setFakeBoldText(true);
		
		levelBarPaint = new Paint();
		levelBarPaint.setARGB(255, 191, 198, 221);
		
		levelBackPaint = new Paint();
		levelBackPaint.setARGB(255, 110, 113, 127);
		
		levelTextPaint = new Paint();
		levelTextPaint.setARGB(255, 191, 198, 221);
		levelTextPaint.setAntiAlias(true);
		levelTextPaint.setTextSize(levelTextSize+4);
		levelTextPaint.setFakeBoldText(true);
		
		TutorialText = new Paint();
		TutorialText.setARGB(255, 191, 198, 221);
		TutorialText.setAntiAlias(true);
		TutorialText.setTextSize(levelTextSize+4);
		TutorialText.setFakeBoldText(true);
		
		linePaint = new Paint();
		linePaint.setARGB(255, 191, 198, 221);
		linePaint.setStrokeWidth(2.0f);
		
		multiplierPaint = new Paint();
		multiplierPaint.setARGB(255, 255, 255, 255);
		multiplierPaint.setTextSize(scoreTextSize);
		multiplierPaint.setAntiAlias(true);
		
		endPaint = new Paint();
		endPaint.setARGB(255, 255, 255, 255);
		endPaint.setTextSize(scoreTextSize);
		endPaint.setAntiAlias(true);
		endPaint.setTextAlign(Paint.Align.CENTER);
		
		// draw a solid green rectangle
		TimeRectangle = new Paint();
		TimeRectangle.setAntiAlias(false);
		TimeRectangle.setColor(Color.GREEN);
		
		//timeTexture  = new Bitmap();
		timeBM = resizeBitmap(GameThread.time_texture, 0.75f);
		for(int i = 0; i < 15; i++){
			timeTexture[i] = timeBM;
		}
		timeBM_red = resizeBitmap(GameThread.time_texture_red, 0.75f);
		timeBM_blue = resizeBitmap(GameThread.time_texture_blue, 0.75f);
		
		levelBarX = (int) ((double) displayWidth*0.06);//10
		levelBarY = (int) ((double) displayHeight*0.036); //5;
		levelBarLength = (int) ((double) displayWidth*0.25); //45;
		levelBarHeight = (int) ((double) displayHeight*0.03); //20;
		levelBarScoreX = (int) ((double) displayWidth - displayWidth*0.08);
		levelBarScoreY = (int) ((double) displayHeight*0.06); //5;
		barCenterX = (int) ((double) displayWidth*0.5);
	}
	
	
	public void addTime(float t){
		if(t>0){
			fillTimeArrayBlue(t);
		}
		time = (float) (time + t);
		if (time > 15.0f){
			time = 15.0f;
		}
		checkTime();
		
	}
	
	public void fillTimeArrayBlue(float t){
		for(int i = 14; i >= time; i--){
			timeTexture[i] = timeBM_blue;
		}
		
		Timer timer = new Timer(); 
        timer.schedule(new TimerTask() {            
        	public void run() { 
        		fillTimeArrayGrey();
        	} 
        }, 2000);
		
	}
	
	public void fillTimeArrayGrey(){
		for(int i = 0; i < 15; i++){
			timeTexture[i] = timeBM;
		}
	}
	
	public void checkTime(){
		
		if(time < 0){
			GameThread.mMode = GameThread.STATE_GAMELOOSE;
		}
	}
	
	public void setTime(float t){
		time =  (float) t;
		
		checkTime();
		
	}
	
	public void addScore(int s){
		addedscore = s*multiplier;
		score = score + s*multiplier;
		levelScore = levelScore + s*multiplier;
		setLevelBarProgress();
	}
	
	public void addLevel(int l){
		currentLevelPoints = nextLevelPoints;
		nextLevelPoints = nextLevelPoints*2;
		level = level + l;
		levelScore = score - currentLevelPoints;
	}
	
	public void setScore(int s){
		score = s;	
	}
	
	public void setLevel(int l){
		level = l;	
	}
	
	public int getLevel(){
		return level;
	}
	
	public int getScore(){
		return score;
	}
	
	public void setMultiplier(int s){
		multiplier = s;
	}
	
	public void addMultiplier(int s){
		multiplier = multiplier + s;
	}
	
	public int getMultiplier(){
		return multiplier;
	}
	
	public void setStreak(int s){
		streak = s;
		if(streak == 0){
			setMultiplier(1);
		}
	}
	
	public void addStreak(int s){
		streak = streak + s;
		if(streak == 9 || streak == 6 || streak == 3){
			GameThread.showDouble = true;
			
			Timer t = new Timer(); 
	        t.schedule(new TimerTask() {            
	        	public void run() { 
	        		GameThread.showDouble = false;
	        	} 
	        }, 4000);
		}
		if(streak == 9){
			setMultiplier(4);
			GameThread.playSound(4);
		}else if(streak == 6){
			setMultiplier(3);
			GameThread.playSound(4);
		}else if(streak == 3){
			setMultiplier(2);
			GameThread.playSound(4);
		}
	}
	
	public int getStreak(){
		return streak;
	}
	
	public void setLevelBarProgress(){
		if(score >= nextLevelPoints)
		{
			addLevel(1);
			GameThread.mMode = 5;
		}
		
		levelBarProgress = (int) ((double) levelBarLength * ((double) (levelScore)/(nextLevelPoints-currentLevelPoints)));
		
		
//		if(score >= levelValues[level] && level<levelValues.length-1){
//			addLevel(1);
//		}
//		
//		levelBarProgress = (int) ((double) levelBarLength * ((double) (score - levelValues[level-1])/(levelValues[level] - levelValues[level-1])));
	}
	
	public Bitmap resizeBitmap(Bitmap bm, float scaleFactor){
		Matrix matrix = new Matrix();
		matrix.postScale(scaleFactor, scaleFactor);
		
		// recreate the new Bitmap and set it back
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        
        return resizedBitmap;
	}
	
	public void printScore(Canvas canvas){
    	//canvas.drawText("Time: " + (float) time + "| Score: "+ score + " | Level:" + level, 10,40, scorePaint);
		
    	Rect rect1 = new Rect(levelBarX,levelBarY,levelBarX+levelBarProgress,levelBarY+levelBarHeight);
    	Rect rect2 = new Rect(levelBarX,levelBarY,levelBarX+levelBarLength,levelBarY+levelBarHeight);
    	canvas.drawRect(rect2, levelBackPaint);
    	canvas.drawRect(rect1, levelBarPaint);
    	
    	canvas.drawText("" + level, levelBarLength + 40 , levelBarHeight + (int)((double) levelTextSize*1.35), levelTextPaint);
    	//20, 22
    	//canvas.drawLine(levelBarX, levelBarY, levelBarX+levelBarLength, levelBarY, linePaint);
    	//canvas.drawLine(levelBarX+levelBarLength, levelBarY, levelBarX+levelBarLength, levelBarY+levelBarHeight, linePaint);
    	//canvas.drawLine(levelBarX, levelBarY+levelBarHeight, levelBarX+levelBarLength, levelBarY+levelBarHeight, linePaint);
    	
    	canvas.drawLine(levelBarX, levelBarY+levelBarHeight+14, displayWidth-levelBarX, levelBarY+levelBarHeight+14, linePaint);
    	//canvas.drawLine(levelBarX, levelBarY, levelBarX, levelBarY+levelBarHeight, linePaint);
    	
    	//canvas.drawText("x" + multiplier, barCenterX, levelBarScoreY, multiplierPaint);
    	if(multiplier == 2){
    		canvas.drawBitmap(GameThread.x2_texture, barCenterX-21, levelBarY-15, null);
    	}else if(multiplier == 3){
    		canvas.drawBitmap(GameThread.x3_texture, barCenterX-21, levelBarY-15, null);
    	} else if(multiplier == 4){
    		canvas.drawBitmap(GameThread.x4_texture, barCenterX-21, levelBarY-15, null);
    	}
    	
    	canvas.drawText("" + score, levelBarScoreX, levelBarScoreY, scorePaint);
	}
	
	public void printTime(Canvas canvas){
			// FLER PLUPPAR MER STRESS :)
		if(time > 4){
			for(int i = 0; i < time; i++){
				//canvas.drawRect( + (i*5) + (i*20) , 5,  ( + (i*25)) + 20, 25, TimeRectangle);
				canvas.drawBitmap(timeTexture[i], (float) ((GameThread.paddingLeft * 0.5) + (i*42*0.7)), 700, null);
			}
		}else{
			for(int i = 0; i < time; i++){
				//canvas.drawRect( + (i*5) + (i*20) , 5,  ( + (i*25)) + 20, 25, TimeRectangle);
				canvas.drawBitmap(timeBM_red, (float) ((GameThread.paddingLeft * 0.5) + (i*42*0.7)), 700, null);
			}
		}
	}
	
	public void drawEndScreen(Canvas canvas){
		canvas.drawBitmap(GameThread.fail_texture, barCenterX -80, 100, null);
		canvas.drawText("You lost! Total score:", barCenterX, 340, endPaint);
		canvas.drawText("" + score, barCenterX, 390, endPaint);
		canvas.drawBitmap(GameThread.restartButton_texture, barCenterX - 100, 480, null);
	}
	
	
	public void handleActionDown(int eventX, int eventY) {
		//TextView tv;
		Log.d("PLAYER", "TRYCK");
		if(GameThread.mMode == 2){
			if ((eventX >= barCenterX - 100) && (eventX < barCenterX + 100)
					&& ((eventY >= 480) && (eventY < 580))) {
				restartButtonPressed = true;
			}
		}
	}
	
	public void drawNewLevel(Canvas canvas){
		if(level!=1){
			canvas.drawBitmap(GameThread.newlevel_texture, barCenterX -90, 170, null);
		}
		canvas.drawText("LEVEL  " +level+"", barCenterX+50, 390, scorePaint);
	}
	
	public void drawScore(Canvas canvas){
		//canvas.drawText("+" +addedscore+"", barCenterX+50, GameThread.displayHeight/2 + 30, scoreTextPaint);
		if(addedscore > 50){
			canvas.drawText("Bonus:", barCenterX-30, GameThread.displayHeight/2 + 30, scoreTextPaint);
			canvas.drawText("+" +(addedscore-50)+"", barCenterX+50, GameThread.displayHeight/2 + 30, scoreTextPaint);
			canvas.drawText("Puzzle:", barCenterX-30, GameThread.displayHeight/2 + 30 + 30, scoreTextPaint);
			canvas.drawText("+50", barCenterX+50, GameThread.displayHeight/2 + 30 + 30, scoreTextPaint);
		}else{
			canvas.drawText("Puzzle:", barCenterX-30, GameThread.displayHeight/2 + 30+30, scoreTextPaint);
			canvas.drawText("+" +addedscore+"", barCenterX+50, GameThread.displayHeight/2 + 30+30, scoreTextPaint);
		}
		/*linePaint.setARGB(255, 62, 206, 206);
		canvas.drawLine(levelBarX, GameThread.displayHeight/2 + 68 , displayWidth-levelBarX, GameThread.displayHeight/2 + 68, linePaint);
		linePaint.setARGB(255, 191, 198, 221);
		scoreTextPaint.setARGB(255, 62, 206, 206);
		canvas.drawText("Total Score:", barCenterX-30, GameThread.displayHeight/2 + 95, scoreTextPaint);
		canvas.drawText("+" +score+"", barCenterX+50, GameThread.displayHeight/2 + 95, scoreTextPaint);
		scoreTextPaint.setARGB(255, 109, 204, 122);*/
		//canvas.drawText("Bonus:  " +(score-50)+"", barCenterX+80, GameThread.displayHeight/2, scoreTextPaint);
	}
	
	public void drawDouble(Canvas canvas){
		scoreTextPaint.setTextAlign(Paint.Align.CENTER);
		if(multiplier == 2){
			scoreTextPaint.setARGB(255, 87, 194, 201);
			canvas.drawText("DOUBLE SCORE", barCenterX ,GameThread.paddingTop-25, scoreTextPaint);
		}else if(multiplier == 3){
			scoreTextPaint.setARGB(255, 199, 87, 199);
			canvas.drawText("TRIPLE SCORE", barCenterX ,GameThread.paddingTop-25, scoreTextPaint);
		} else if(multiplier == 4){
			scoreTextPaint.setARGB(255, 214, 73, 73);
			canvas.drawText("QUADRUPLE SCORE", barCenterX ,GameThread.paddingTop-25, scoreTextPaint);
		}
		scoreTextPaint.setARGB(255, 109, 204, 122);
		scoreTextPaint.setTextAlign(Paint.Align.RIGHT);
	}
	
}
